package com.coolgor.axnut.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class NetworkUtil {

    /**
     * 调用ping命令
     * @param hostname
     * @return
     */
    public static final boolean isNodeReachable(String hostname) {
        try {
//            return 0==Runtime.getRuntime().exec("ping -c 1 "+hostname).waitFor();
            return 0==Runtime.getRuntime().exec("ping "+hostname).waitFor();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static  final boolean inetPing(String ip) {
        boolean isIpReachable = false;
        InetAddress address;
        try
        {
            address = InetAddress.getByName(ip);
            isIpReachable = address.isReachable(3000);
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
            return false;
        } catch (IOException e)
        {
            e.printStackTrace();
            return false;
        }
        return isIpReachable;
    }
}
